package mus2.mote;

/*
 * UNPUBLISHED PROPRIETARY INFORMATION
 * Copyright (c) 2007-2008 Sentilla Corporation
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Sentilla Corporation ("Confidential Information"). Disclosure of this
 * Confidential Information is prohibited, and its use shall be in
 * compliance with terms of the license agreement with Sentilla Corporation.
 */

import javax.measure.quantity.ElectricPotential;

import com.sentilla.net.Sender;
import com.sentilla.net.SenderDriver;
import com.sentilla.platform.tmote.McuVoltage;
import com.sentilla.platform.tmote.VoltageAdc;


/**
 * A basic sense and send application that processes data from a 
 * SparkFun UltraSonic Rangefinder and sends back a packet with the 
 * distance 
 *
 * @author <a href="mailto:dan@sentilla.com">Dan Steingart</a>
 */
public class UltraSonicMoteApp {
    public static void motemain() throws InterruptedException {
        //Make sensors for ADC2, Battery Level and acceleration
        VoltageAdc us = new VoltageAdc(VoltageAdc.PORT_A2);
        McuVoltage mcu = new McuVoltage();
        
        //Make a Sender
        Sender sender = SenderDriver.create("local");
        
        // Instantiate new message, which we wil continually update and send
        UltraSonicMessage usMsg = new UltraSonicMessage();
        
        // Start a single infinite loop
        while (true) {           
        	// read and processs UltraSonic Sesnor             
            double vdist = us.read().doubleValue(ElectricPotential.UNIT);
            double potential = mcu.read().doubleValue(ElectricPotential.UNIT);
            usMsg.meter = (vdist*12.977723) / (potential - 0.0076129);
                
            //Send Message
            sender.send(usMsg);
        }
    }

}
